import token from "@/utils/token";

const auth = {

    hasAll: () => {
        const ALL_PERMISSION = "*:*:*"
        const powerCodeList = token.getPowerCodeList() || [];
        if (!powerCodeList)
            return false;
        return powerCodeList.includes(ALL_PERMISSION);
    },

    hasPower: (powerCode) => {
        if (auth.hasAll()) return true;
        if (!powerCode || powerCode === '')
            return false;
        const powerCodeList = token.getPowerCodeList() || [];
        if (!powerCodeList)
            return false;
        return powerCodeList.includes(powerCode);
    },

    hasAnyPower: (powers) => {
        if (auth.hasAll()) return true;
        if (!powers || powers.length === 0)
            return false;
        const powerCodeList = token.getPowerCodeList();
        if (!powerCodeList)
            return false;
        return powers.some(power => powerCodeList.includes(power));
    },

    hasAllPower: (powers) => {
        if (auth.hasAll()) return true;
        if (!powers || powers.length === 0)
            return false;
        const powerCodeList = token.getPowerCodeList();
        if (!powerCodeList)
            return false;
        return powers.every(power => powerCodeList.includes(power));
    },

    hasRole: (roleCode) => {
        if (auth.hasAll()) return true;
        if (!roleCode || roleCode === '')
            return false;
        const roleCodeList = token.getRoleCodeList() || [];
        if (!roleCodeList)
            return false;
        return roleCodeList.includes(roleCode);
    },

    hasAnyRole: (roles) => {
        if (auth.hasAll()) return true;
        if (!roles || roles.length === 0)
            return false;
        const roleCodeList = token.getRoleCodeList() || [];
        if (!roleCodeList)
            return false;
        return roles.some(role => roleCodeList.includes(role));
    },

    hasAllRole: (roles) => {
        if (auth.hasAll()) return true;
        if (!roles || roles.length === 0)
            return false;
        const roleCodeList = token.getRoleCodeList() || [];
        if (!roleCodeList)
            return false;
        return roles.every(role => roleCodeList.includes(role));
    },

}

export default auth;